package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.AddressSelectBean;
import cn.bocweb.spinning.model.Bean.CompanySearchLocationBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.adapter.ProvinceAdapter;
import cn.bocweb.spinning.view.adapter.SearchLocationCommonAdapter;
import cn.bocweb.spinning.view.adapter.SearchLocationTogetherAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class AddressSelectActivity extends BaseActivity {

    AddressSelectBean addressSelectBean;
    Map<String, List<CompanySearchLocationBean.ProvinceBean.CityBean>> provinceMap;
    List<CompanySearchLocationBean.ProvinceBean> provinceList;
    SearchLocationCommonAdapter searchLocationCommonAdapter;
    ProvinceAdapter provinceAdapter;
    UserPresenter presenter;
    List<CompanySearchLocationBean.CommonBean> commonBeenList;
    List<CompanySearchLocationBean.TogetherBean> togetherBeenList;
    SearchLocationTogetherAdapter searchLocationTogetherAdapter;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.address_select_list)
    ExpandableListView addressSelectList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_select);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        addressSelectBean = new AddressSelectBean();
        provinceMap = new HashMap<>();
        provinceList = new ArrayList<>();
        commonBeenList = new ArrayList<>();
        presenter = new UserPresenterImp();
        togetherBeenList = new ArrayList<>();

        searchLocationCommonAdapter = new SearchLocationCommonAdapter(this, commonBeenList);
        searchLocationTogetherAdapter = new SearchLocationTogetherAdapter(this, togetherBeenList);
        provinceAdapter = new ProvinceAdapter(provinceMap, this , provinceList);

        initToolbar();
        initList();

        showLoading();
        presenter.getCompanySearchLocation();
    }

    public void onEventMainThread(CompanySearchLocationBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            commonBeenList.addAll(bean.getCommon());
            togetherBeenList.addAll(bean.getTogether());

            searchLocationTogetherAdapter.notifyDataSetChanged();
            searchLocationCommonAdapter.notifyDataSetChanged();

            provinceList.addAll(bean.getProvince());
            for (CompanySearchLocationBean.ProvinceBean p : provinceList) {
                provinceMap.put(p.getTitle(), p.getCity());
            }

            provinceAdapter.notifyDataSetChanged();
        }
    }

    private void initList() {
        View headerView = initHeader();

        addressSelectList.addHeaderView(headerView);
        addressSelectList.setAdapter(provinceAdapter);
        addressSelectList.setGroupIndicator(null);
        addressSelectList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                TextView tv = (TextView)v.findViewById(R.id.tv_city);
                Intent intent = new Intent();
                intent.putExtra(Constant.SEARCH_ADDRESS_TITLE, tv.getText().toString());
                setResult(Constant.SEARCH_ADDRESS_TO_RESULT, intent);
                finish();
                return false;
            }
        });
    }

    /**
     * gridview 的头部
     */
    private View initHeader() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_address_select_header, null);
        GridView searchAddressAlways = (GridView) view.findViewById(R.id.search_address_always);
        GridView searchAddressFocus = (GridView) view.findViewById(R.id.search_address_focus);

        searchAddressAlways.setAdapter(searchLocationCommonAdapter);
        searchAddressFocus.setAdapter(searchLocationTogetherAdapter);

        searchAddressAlways.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(Constant.SEARCH_ADDRESS_TITLE, commonBeenList.get(position).getTitle());
                setResult(Constant.SEARCH_ADDRESS_TO_RESULT, intent);
                finish();
            }
        });

        searchAddressFocus.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(Constant.SEARCH_ADDRESS_TITLE, togetherBeenList.get(position).getTitle());
                setResult(Constant.SEARCH_ADDRESS_TO_RESULT, intent);
                finish();
            }
        });

        return view;
    }

    private void initToolbar() {
        toolbar.setTitle("筛选地址");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
